<template>
  <div class="gray-page record-page log-page">
    <nav-bar title="网格员列表" theme="white">
      <template #custom>
        <img src="@/assets/images/cockpit/home/status-bar-search.png" class="iconimg" alt="" @click="filterClick" />
      </template>
    </nav-bar>
    <div class="page-main" style="margin-top: 44px">
      <div class="table-list">
        <van-sticky :offset-top="44">
          <div class="table-list-head">
            <van-row>
              <van-col :span="6" align="center">姓名</van-col>
              <van-col :span="4" align="center">性别</van-col>
              <van-col :span="4" align="center">类别</van-col>
              <van-col :span="10" align="center">所属区域</van-col>
            </van-row>
          </div>
        </van-sticky>
        <div class="page-scroll">
          <van-sticky :offset-top="88">
            <van-pull-refresh v-model="refreshing" style="min-height: 100%">
              <template v-if="listData.length">
                <van-list
                  v-model="loading"
                  :finished="finished"
                  :immediate-check="false"
                  :error.sync="error"
                  error-text="请求失败，点击重新加载"
                  finished-text="没有更多了~"
                  @load="getList"
                >
                  <div v-for="(item, key) in listData" :key="key">
                    <div
                      class="table-list-row"
                      :class="key % 2 ? 'table-list-row__even' : 'table-list-row__odd'"
                      @click="toDetail(item)"
                    >
                      <van-row type="flex">
                        <van-col :span="6">
                          <p class="van-ellipsis table-list-row__center">
                            {{ item.gridPersonName }}
                          </p>
                        </van-col>
                        <van-col :span="4">
                          <p class="van-ellipsis table-list-row__center">{{ item.sexName }}</p>
                        </van-col>
                        <van-col :span="4" align="center">
                          <p class="van-ellipsis table-list-row__center">{{ item.gridPersonTypeName }}</p>
                        </van-col>
                        <van-col :span="10" align="center">
                          <p class="van-ellipsis table-list-row__center">{{ item.community }}</p>
                        </van-col>
                      </van-row>
                    </div>
                  </div>
                </van-list>
              </template>
              <template v-else>
                <van-empty
                  :image="require('@/assets/images/basic/pic_no_content.png')"
                  description="暂无内容~"
                  style="height: 300px"
                />
              </template>
            </van-pull-refresh>
          </van-sticky>
        </div>
      </div>
    </div>
    <van-popup v-model="popUpshow" position="right" :style="{ height: '100%', width: '90%' }">
      <div class="pop-top">
        <p>筛选</p>
        <p @click="emptyClick">清空</p>
      </div>
      <div class="pop-content">
        <p class="pop-title">行政区划</p>
        <van-tree-select
          :style="{ height: '92%' }"
          :items="items"
          :active-id.sync="items.activeId"
          :main-active-index.sync="activeIndex"
          @click-nav="onNavClick"
        >
          <template #content>
            <ul class="right-content">
              <li v-for="(item, index) in secondarymenuList" :key="index">
                <p>{{ item.regionName }}</p>
                <p>{{ item.eventCount }}</p>
              </li>
            </ul>
          </template>
        </van-tree-select>
      </div>
      <div class="pop-bottom">
        <button @click="emptyClick">清空</button>
        <button @click="determine">确定</button>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import List from '@/mixins/list'
import Cockpit from '@/api/cockpit'
export default {
  mixins: [List],
  data() {
    return {
      api: Cockpit.GridMember,
      searchForm: {
        pageNumber: 1,
        recordSize: 0,
        pageSize: 20,
        regionCode: ''
      },
      popactive: 0,
      activeIndex: 0,
      popUpshow: false,
      treeactive: 0,
      items: [],
      secondarymenu: '', // 二级菜单传参数
      secondarymenuList: [] // 二级菜单数据
    }
  },
  computed: {
    ...mapState({
      user: state => state.userInfo
    })
  },
  created() {
    this.searchForm.regionCode = this.user.regionCode
    this.getList()
  },
  methods: {
    filterClick() {
      // 筛选
      this.adlmenu()
      this.searchForm.regionCode = this.user.regionCode
      this.popUpshow = true
    },
    // 清空
    emptyClick() {
      this.searchForm.regionCode = this.user.regionCode
    },
    determine() {
      this.popUpshow = false
      this.getList(1)
    },
    // 点击左边菜单
    onNavClick(index) {
      const substationCode = this.items[index].activeId
      this.secondarymenu = substationCode
      this.searchForm.regionCode = substationCode
      this.secondarymenuClick()
    },
    // 二级菜单
    secondarymenuClick() {
      Cockpit.Online.queryRegion({ communityRegion: this.secondarymenu }).then(res => {
        const data = { ...res.data }
        if (data.status === 0) {
          const reversedata = data.data.reverse()
          this.secondarymenuList = reversedata
        } else {
          this.$toast(data.message)
        }
      })
    },
    adlmenu() {
      this.items = []
      Cockpit.Online.queryRegion({ regionCode: this.user.regionCode }).then(res => {
        const data = { ...res.data }
        if (data.status === 0) {
          const reversedata = data.data.reverse()
          this.secondarymenu = reversedata[0].regionCode
          this.searchForm.regionCode = reversedata[0].regionCode
          for (let i = 0; i < reversedata.length; i++) {
            this.items.push({
              activeId: reversedata[i].regionCode,
              text: reversedata[i].regionName
            })
          }
          this.secondarymenuClick()
        } else {
          this.$toast(data.message)
        }
      })
    },
    toDetail(data) {
      this.$router.push({
        path: '/cockpit/gridMember/detail',
        query: {
          gridPersonCode: data.gridPersonCode,
          appCode: data.appCode
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
@import '~@/assets/style/list.less';
@import '~@/assets/style/table.less';
@import '~@/assets/style/cockpit/gridmember.less';
</style>
